﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.IO;

namespace Historia.Formulaires
{
    /// <summary>
    /// Interaction logic for frmLieu.xaml
    /// </summary>
    public partial class frmLieu : Window
    {
        #region Membres

        protected DataManager _dm = null;
        protected Int32? _idLieu = null;
        protected DataRow _dr = null;

        #endregion

        #region Constructeur

        public frmLieu()
        {
            InitializeComponent();
        }

       
        #endregion

        #region Méthodes

        public bool? ShowFormDialog(ref DataManager dataManager, Int32? idLieu)
        {
            this._dm = dataManager;
            this._idLieu = idLieu;
            this.InitData();
            this.LoadData();
            this.DisplayData();
            return this.ShowDialog();
        }

        protected void InitData() {
            if (this._idLieu.HasValue)
            {
                this._dr = this._dm.DB.Tables[DataManager.LIEUX].Select("id_lieu = " + this._idLieu.ToString()).First();
            }
            else {
                this._dr = this._dm.DB.Tables[DataManager.LIEUX].NewRow();
            }
        }

        protected void LoadData() {
            // On charge les catégories
            this.cboCategorie.Items.Clear();

            var query = from tbl in this._dm.DB.Tables[DataManager.LIEUX].AsEnumerable()
                        group tbl by tbl.Field<string>("categorie") into groupedTbl
                        select groupedTbl;

            foreach (var dr in query)
            {
                this.cboCategorie.Items.Add(new ComboBoxItem() { Content = dr.Key });
            }
        }

        protected void DisplayData() {
            this.txtNom.Text = this._dr.Field<string>("nom");
            this.cboCategorie.Text = this._dr.Field<string>("categorie");
            this.txtDescription.Text = this._dr.Field<string>("description");
            this.txtNotes.Text = this._dr.Field<string>("notes");
            this.AfficherImage();
            
        }

        protected void ReadData() {
            
            this._dr.SetField<string>("nom", this.txtNom.Text);
            this._dr.SetField<string>("categorie", this.cboCategorie.Text);
            this._dr.SetField<string>("description", this.txtDescription.Text);
            this._dr.SetField<string>("notes", this.txtNotes.Text);

            
        }

        protected void ChangerImage()
        {
            // Configure la fenêtre d'ouverture
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".jpg"; // Default file extension
            dlg.Filter = "Toute les images |*.bmp;*.gif;*.jpg;*.png|BMP |*.bmp|GIF |*.gif|JPEG |*.jpg|PNG |*.png"; // Filter files by extension

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                string newFileName = "lieu_" + this.txtNom.Text + dlg.FileName.Substring(dlg.FileName.LastIndexOf("."));
                if (this._dm.CopierFichier(dlg.FileName, this._dr.Field<string>("photo"), newFileName))
                {
                    this._dr.SetField<string>("photo", newFileName);
                    this.AfficherImage();
                }
            }
        }

        protected void RemoveImage()
        {
            this._dm.RemoveFile(this._dr.Field<string>("photo"));
            this._dr.SetField<string>("photo", null);
            this.AfficherImage();

        }

        protected void AfficherImage()
        {

            //On affiche l'image
            if (!string.IsNullOrEmpty(this._dr.Field<string>("photo")))
            {
                this.imgLieu.Source = this._dm.GetImage(this._dr.Field<string>("photo"));
            }
            else
            {
                this.imgLieu.Source = new BitmapImage(new Uri("pack://application:,,,/Historia;component/Images/ImageNone.png"));
            }

        }

        #endregion

        private void btnAnnuler_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.DialogResult = false;
                this._dm.DB.Tables[DataManager.LIEUX].RejectChanges();
                this.Close();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.ReadData();
                if (!this._idLieu.HasValue)
                {
                    this._dm.DB.Tables[DataManager.LIEUX].Rows.Add(this._dr);
                }
                this._dm.DB.Tables[DataManager.LIEUX].AcceptChanges();
                this.DialogResult = true;
                this.Close();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void btnChangerImage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.ChangerImage();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void btnRemoveImage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.RemoveImage();
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
